import BulletCtr from "./BulletCtr";

export default class BulletManager {
    //在这里管理子弹
    bulletPool: cc.NodePool = null;
    bulletBombPool: cc.NodePool = null;
    coinPool: cc.NodePool = null;
    prefabCache: Map<string, cc.Prefab> = new Map<string, cc.Prefab>();

    bulletBomb: cc.Node = null;
    

    /**子弹类型合图 */
    bulletAtlas: cc.SpriteAtlas = null;

    testID: number = 0;//给每个子弹加上id

    constructor() {
        this.bulletPool = new cc.NodePool("BulletCtr")
        this.bulletBombPool = new cc.NodePool()
        this.coinPool = new cc.NodePool() //为了省事 都写这个文件里
        cc.resources.load('image/bullet', cc.SpriteAtlas, (err, atlas: cc.SpriteAtlas) => {
            if (err) {
                return
            }
            this.bulletAtlas = atlas
        })
    }

    public getBulletAtlas(): cc.SpriteAtlas {
        return this.bulletAtlas
    }

    public putBullet(bullet: cc.Node) {
        this.bulletPool.put(bullet)
    }

    public putBulletBomb(bomb:cc.Node){
        this.bulletBombPool.put(bomb)
    }

    public putCoin(coin:cc.Node){
        this.coinPool.put(coin)
    }

    /**
     * 
     * @param type 预制体路径
     * @returns cc.Prefab
     */
    async getBulletPrefab(type: string): Promise<cc.Prefab> {
        // 在这里动态生成子弹
        return new Promise(resolve => {
            if (this.prefabCache.has(type)) {
                // console.log('从 map取出prefab')
                resolve(this.prefabCache.get(type))
                return
            }
            cc.resources.load(type, cc.Prefab, (err, fab: cc.Prefab) => {
                if (err) {
                    resolve(null)
                    return console.log('子弹加载错误 ', err)
                }
                this.prefabCache.set(type, fab)
                // console.log('异步加载 取出prefab')
                fab.addRef()
                resolve(fab)
            })
        })
    }

    async getBullet(type: string): Promise<cc.Node> {
        //在这里动态生成子弹
        if (this.bulletPool.size() > 0) {
            return this.bulletPool.get()
        }
        let fab = await this.getBulletPrefab(type)
        let bullet = cc.instantiate(fab)
        return bullet
    }

    getBomb(type: string): Promise<cc.Node> {
        //在这里动态生成子弹爆炸效果
        return new Promise(resolve => {
            if (this.bulletBombPool.size() > 0) {
                // console.log('从 map取出prefab')
                resolve(this.bulletBombPool.get() )
                return
            }
            cc.resources.load(type, cc.Prefab, (err, fab: cc.Prefab) => {
                if (err) {
                    resolve(null)
                    return console.log('子弹加载错误 ', err)
                }
                this.prefabCache.set(type, fab)
                fab.addRef()
                resolve(cc.instantiate(fab))
            })
        })

    }

    getCoin(type: string): Promise<cc.Node> {
        //在这里动态生成子弹爆炸效果
        return new Promise(resolve => {
            if (this.coinPool.size() > 0) {
                // console.log('从 map取出prefab')
                resolve(this.coinPool.get() )
                return
            }
            cc.resources.load(type, cc.Prefab, (err, fab: cc.Prefab) => {
                if (err) {
                    resolve(null)
                    return console.log('子弹加载错误 ', err)
                }
                this.prefabCache.set(type, fab)
                fab.addRef()
                resolve(cc.instantiate(fab))
            })
        })

    }

    clearCache() {
        this.prefabCache.forEach((value, key) => {
            value.decRef()
        })
    }
}
export const bulletManager = new BulletManager()
